/*
 * Container.cpp
 */

#include "Container.h"

Container::Container() {}

Container::Container(Sint16 posX, Sint16 posY, Uint16 w, Uint16 h)
:Widget(posX, posY, w, h) {}

Container::~Container() {
	std::list<Widget*>::iterator it;
	for (it = widgets.begin(); it != widgets.end(); ++it)
	{
		if ( (*it)->isManaged() )
			delete *it;
	}
	widgets.clear();
}

void Container::add(Widget& widget) {
	this->widgets.push_back(&widget);
}

void Container::show()
{
	this->showAllChildren();
}

void Container::showAllChildren()
{
    std::list<Widget*>::iterator it;
    for (it = widgets.begin(); it != widgets.end(); ++it)
    {
    	(*it)->show();
    }
}

void Container::draw(SDL_Surface* screen)
{
    std::list<Widget*>::iterator it;
    for (it = widgets.begin(); it != widgets.end(); ++it)
    {
    	if ((*it)->mustBeShown())
    		(*it)->draw(screen);
    }
}

void Container::broadcastEvents(SDL_Event* event)
{
    std::list<Widget*>::iterator it;
    for (it = widgets.begin(); it != widgets.end(); ++it)
    {
    	if ((*it)->mustBeShown())
    		(*it)->handleEvent(event);
    }
}
